

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Map {

	private int[][] info;
	private String mode;
	private RoadMap[][] map;
	private JPanel frame;
	private Draw draw;
	
	private double version;
	private int filtering_id;
	private BuildingList BL;
	private Road[] Roads;
	
	public Map () {
		this.version = 0.0;
		this.filtering_id = -999;
		this.BL = new BuildingList();
		this.Roads = new Road[225];
		info=new int[15][15];
	}
	
	public int[][] makeInfo()
	{
		for(int i=0; i<BL.list.size(); i++)
		{
			int cnt=BL.list.get(i).getNum_of_RoadMap();
			for(int j=0; j<cnt; j++)
			{	int x=BL.list.get(i).getRm(j).getXY()[0];
				int y=BL.list.get(i).getRm(j).getXY()[1];
				int c_id=BL.list.get(i).getRm(j).getChildId();
				info[x][y]=c_id;
			}
		}
		int i=0;
		while(Roads[i]!=null)
		{
			int cnt=Roads[i].getNum_of_rm();
			for(int j=0; j<cnt; j++)
			{	int x=Roads[i].getRoadMap(j).getXY()[0];
				int y=Roads[i].getRoadMap(j).getXY()[1];
				int c_id=Roads[i].getRoadMap(j).getChildId();
				info[x][y]=c_id;
			}
			i++;
		}
			
		return info;
	}
	
	public void init (int[][] __info) {
		this.info = __info;
	}
	
	public void filtering (int __filtering_id) {
		this.filtering_id = __filtering_id;
	}
	
	public void setMode(String __mode) {
		this.mode = __mode;
	}

	public String getMode() {
		return this.mode;
	}
	
	public void load (int x, int y, int child_id) {
		int[] indexs = getMapIndex(x, y, mode);
		
		if (mode == "surrounding") {
			map = new RoadMap[13][];

			System.out.println("Original Position:" + x + "|" + y);
			System.out.println(indexs[0] + "|" + indexs[1] + "->" + indexs[2] + "|" + indexs[3] + ", position:" + indexs[4] + "|" + indexs[5]);
			
			for (int row=0; row<13; row++) {
				map[row] = new RoadMap[7];
				
				for (int col=0; col<7; col++) {
					int id = info[indexs[0]+row][indexs[1]-col];
					map[row][col] = new RoadMap(id);
					
					if (Main.Navi.Map.BL.search(indexs[0]+row, indexs[1]-col) != null) {
						map[row][col].setBuildName(Main.Navi.Map.BL.search(indexs[0]+row, indexs[1]-col).getName());
					}
				}
			}
			
			map[indexs[4]][indexs[5]] = new RoadMap(child_id);
		}
		else {
			map = new RoadMap[15][];
			
			for (int row=0; row<15; row++) {
				map[row] = new RoadMap[15];
				
				for (int col=0; col<15; col++) {
					int id = info[indexs[0]+row][indexs[1]-col];
					
					if (filtering_id != -999 && id >= 200 && id < 300 && id != filtering_id) {
						id = 299;
					}
					
					map[row][col] = new RoadMap(id);

					if (Main.Navi.Map.BL.search(indexs[0]+row, indexs[1]-col) != null) {
						map[row][col].setBuildName(Main.Navi.Map.BL.search(indexs[0]+row, indexs[1]-col).getName());
					}				
				}
			}
			
			map[indexs[4]][indexs[5]] = new RoadMap(child_id);
		}
	}
	
	public RoadMap getRoadMap(int x, int y) {
		return this.map[x][y];
	}
	
	private int[] getMapIndex(int x, int y, String mode) {
		int diff_x = 0;
		int diff_y = 0;
		
		if (mode == "all") {
			diff_x = 7;
			diff_y = 7;
		}
		else {
			diff_x = 6;
			diff_y = 3;
		}		
		
		int __start_x = x-diff_x;
		int __start_y = y+diff_y;
		int __last_x = x+diff_x;
		int __last_y = y-diff_y;
		int __x = diff_x;
		int __y = diff_y;
		
		int[] indexs = new int[6];
		
		if (__start_x < 0) {
			__start_x = 0;
			
			__last_x += (diff_x-x);
			__x -= (diff_x-x);
		}
		
		if (__start_y > info[0].length - 1) {
			__last_y -= (__start_y - (info[0].length - 1));
			__y -= (__start_y - (info[0].length - 1));
			
			__start_y = info[0].length - 1;
		}
		
		if (__last_x > info.length - 1) {
			__start_x -= (__last_x - (info.length -1));
			__x += (__last_x - (info.length -1));
			
			__last_x = info.length - 1;
		}
		
		if (__last_y < 0) {
			__last_y = 0;
			
			__start_y += (diff_y-y);
			__y += (diff_y-y);
		}
		
		
		indexs[0] = __start_x;
		indexs[1] = __start_y;
		indexs[2] = __last_x;
		indexs[3] = __last_y;
		indexs[4] = __x;
		indexs[5] = __y;
		
		return indexs;
	}
	
	public JPanel setPanel(int x, int y, int width, int height) {
		this.draw = new Draw(this, map);
		this.draw.setSize(x, y, width, height);
		this.draw.setFocusable(true);
		
	    return this.draw;
	}
	
	public void start() {
	}
	
	public void draw() {
		this.draw.get(map);
		this.draw.revalidate();
		//this.draw.repaint();
		this.draw.repaint(0, 0, 0, 800, 420);
		this.draw.requestFocus();
	}
	
	/*Map update function*/
	public void updateMap(Map m)
	{
		setVersion(m.getVersion());
		setBuildingList(m.getBuildingList());
		setRoads(m.getRoads());
	}
	
	//get set
	public double getVersion() {
		return version;
	}
	
	public BuildingList getBuildingList() {
		return BL;
	}
	
	public Road[] getRoads() {
		return Roads;
	}
	
	public void setVersion(double v) {
		version=v;
	}
	
	public void setBuildingList(BuildingList n_bl) {
		BL=n_bl;
	}
	
	public void setRoads(Road[] n_roads) {
		Roads=n_roads;
	}
	
	public Building searchBuilding (String name) {
		return Main.Navi.Map.BL.search(name);
	}
	
	/*Find and return a road*/
	public Road returnRoad(int road_id)
	{
		int i=0;
		while(Roads[i]!=null)
		{
			if(Roads[i].getId()==road_id) break;
			
			i++;
		}
		return Roads[i];
	}	
	
	public void up() {
		int[] position = Main.Navi.getLoc();
		int child_id = 0;
		
		if (position[1] + 1 < this.info[0].length) {
			child_id = this.info[position[0]][position[1]+1];
			
			if (child_id >= 100 && child_id < 200) {
				position[1] += 1;
				child_id = 99 - child_id;
			}
			else {
				child_id = Main.Navi.getMyChildId();
			}
		}
		else {
			child_id = Main.Navi.getMyChildId();
		}
		
		//Main.Navi.setLoc(position[0], position[1]);
		Main.Navi.setMyChildId(child_id);
		this.load(position[0], position[1], child_id);
		
		System.out.println("up:"+position[0]+"|"+position[1]);
		
		this.draw();
	}

	public void down() {
		int[] position = Main.Navi.getLoc();
		int child_id = 0;
		
		if (position[1] - 1 > -1) {
			child_id = this.info[position[0]][position[1]-1];
			
			if (child_id >= 100 && child_id < 200) {
				position[1] -= 1;
				child_id = 99 - child_id;
			}
			else {
				child_id = Main.Navi.getMyChildId();
			}
		}
		else {
			child_id = Main.Navi.getMyChildId();
		}
		
		
		Main.Navi.setMyChildId(child_id);
		this.load(position[0], position[1], child_id);
	
		System.out.println("down:"+position[0]+"|"+position[1]);

		this.draw();		
	}
	
	public void left() {
		int[] position = Main.Navi.getLoc();
		int child_id = 0;
		
		if (position[0] - 1 > -1) {
			child_id = this.info[position[0]-1][position[1]];
			
			if (child_id >= 100 && child_id < 200) {
				position[0] -= 1;
				child_id = 99 - child_id;
			}
			else {
				child_id = Main.Navi.getMyChildId();
			}			
		}
		else {
			child_id = Main.Navi.getMyChildId();
		}
		
		Main.Navi.setMyChildId(child_id);
		this.load(position[0], position[1], child_id);

		System.out.println("left:"+position[0]+"|"+position[1]);
		
		this.draw();
	}

	public void right() {
		int[] position = Main.Navi.getLoc();
		int child_id = 0;
		
		if (position[0] + 1 < this.info.length) {
			child_id = this.info[position[0]+1][position[1]];
			
			if (child_id >= 100 && child_id < 200) {
				position[0] += 1;
				child_id = 99 - child_id;
			}
			else {
				child_id = Main.Navi.getMyChildId();
			}			
		}
		else {
			child_id = Main.Navi.getMyChildId();
		}
		
		Main.Navi.setMyChildId(child_id);
		this.load(position[0], position[1], child_id);

		System.out.println("right:"+position[0]+"|"+position[1]);
		
		this.draw();		
	}
}
